<?php

class Application_Model_IzdelkiMapper
{
    
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Izdelki');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Izdelki $izdelek)
    {
        $data = array(
            'idprodukt' => $izdelek->getIdProdukt(),
            'ime'   => $izdelek->getIme(),
            'cena' => $izdelek->getCena(),
            'zaloga' => $izdelek->getZaloga(),
            'thumbnail' => $izdelek->getThumbNail(),
            'base_image' => $izdelek->getBaseImage(),
        );

        try {
            $novIzdelek = new Application_Model_Izdelki();
            $this->find($data['idprodukt'], $novIzdelek);
            if ($novIzdelek != null && $data['idprodukt']) {
                $where = $this->getDbTable()->getAdapter()->quoteInto('idprodukt = ?', $data['idprodukt']);
                $this->getDbTable()->update($data, $where);
            } else {
                $this->getDbTable()->insert($data);
            }
        } catch (Exception $e) {
            echo "Error: ".$e->getMessage();
            die;
        }

    }

    public function find($id, Application_Model_Izdelki &$izdelek)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();

        $izdelek->setIdProdukt($row->idprodukt)
            ->setIme($row->ime)
            ->setCena($row->cena)
            ->setZaloga($row->zaloga)
            ->setThumbNAil($row->thumbnail)
            ->setBaseImage($row->base_image)
            ->setAktiviran($row->aktiviran);

    }

    public function fetchRow($where) {
        return $this->getDbTable()->fetchRow($where);
    }
    
    
    public function fetchAll($where) {
        return $this->getDbTable()->fetchAll($where);
    }
    
    
    
    public function delete($id)
    {
       $where = $this->getDbTable()->getAdapter()->quoteInto('idprodukt = ?', $id);
       return $this->getDbTable()->delete($where);
    }
    
    
    public function getAllIzdelki($where = null) {
        $izdelkiArray = $this->fetchAll($where);
        $entries = array();


        foreach ($izdelkiArray as $izdelek) {
            $entry = new Application_Model_Izdelki();
            $entry->setIdProdukt($izdelek['idprodukt']);
            $entry->setIme($izdelek['ime']);
            $entry->setCena($izdelek['cena']);
            $entry->setZaloga($izdelek['zaloga']);
            $entry->setThumbNAil($izdelek['thumbnail']);
            $entry->setBaseImage($izdelek['base_image']);
            $entry->setAktiviran($izdelek['aktiviran']);
            $entries[$izdelek['idprodukt']] = $entry;
        }
        return $entries;
    }
    
    
    public function update($idprodukt, $polje, $vrednost) {
        $where = $this->getDbTable()->getAdapter()->quoteInto('idprodukt = ?', $idprodukt);
        $this->getDbTable()->update(array($polje => $vrednost), $where);
    }
    
    public function fetchAllAsArray($where) {
        $result = array();
        $izdelkiArray = $this->fetchAll($where);
        foreach ($izdelkiArray as $izdelek) {
            $izdelekArr = array();

            $izdelekArr['id'] = $izdelek['idprodukt'];
            $izdelekArr['ime'] = $izdelek['ime'];
            $izdelekArr['cena'] = $izdelek['cena'];
            $izdelekArr['zaloga'] = $izdelek['zaloga'];
            $izdelekArr['thumbnail'] = $izdelek['thumbnail'];
            $izdelekArr['base_image'] = $izdelek['base_image'];
            $izdelekArr['aktiviran'] = $izdelek['aktiviran'];

            $izdelekArr['url'] = "seznam/podrobnosti/?id=".base64_encode($izdelek['idprodukt']);

            $result[] = $izdelekArr;
        }

        return $result;
    }
    
    


}

